<?php
class AddUserForm extends Form
{
    function AddUserForm()
    {
        Form::Form('AddUser');

        $this->link_js('javascript/jquery/packed/jquery.validate.js');
        $this->link_js('javascript/jquery/packed/jquery.jeditable.js');
        $this->link_js("javascript/useradmin.js");
        $this->link_js('javascript/rounded-corners.js');
        $this->link_js('javascript/form-field-tooltip.js');
        $this->link_css('style/form-field-tooltip.css');

        $this->link_css("style/paging.css");
        $this->link_css('style/jquery-ui-1.9.2.custom.css');
        $this->link_css('style/msgBoxLight.css');
        $this->link_js('javascript/jquery/jquery-ui-1.9.2.custom.min.js');
        //
        $this->link_js("javascript/jquery/jquery.ui.core.js");
        $this->link_js("javascript/jquery/jquery.ui.widget.js");
        $this->link_js("javascript/jquery/jquery.ui.mouse.js");
        $this->link_js("javascript/jquery/jquery.ui.sortable.js");
        $this->link_js("javascript/jquery/jquery.ui.datepicker.js");
        $this->link_js("javascript/jquery/jquery.msgBox.js");
        CGlobal::$website_title = "Thêm người dùng mới";
    }

    function on_submit()
    {
        if(User::is_root()){//Check root mới được thêm
            $email 	 = trim(Url::get('email'));
            $password = md5(trim(Url::get('password')));
            $full_name 	 = trim(Url::get('full_name'));
            $mobile_phone = trim(Url::get('mobile_phone'));
            $birth_day = trim(Url::get('birth_day'));
            $gender = Url::get('gender');
            $address = trim(Url::get('address'));
            $website = trim(Url::get('website'));
            $yahoo_id = trim(Url::get('yahoo_id'));
            $skype_id = trim(Url::get('skype_id'));

            //Phân nhóm cho user
            $gids = '';
            /*foreach (Url::get('gids') as $a){
                $gids.=$a.'|';
            }*/
            //Upload avatar
            if($_FILES["avatar"]["name"]){
                $avatar = $_FILES["avatar"]["name"];
                if($avatar!=''){
                    if($_FILES["avatar"]["tmp_name"]){
                        if(Upload::storage_check_dir("user_ava/", true, IMAGE_SERVER_NO))
                        {
                            $img_ext = explode(".", $avatar);
                            $file_ext = array_pop($img_ext);
                            if(!in_array($file_ext,array('jpg','png','jpeg','gif'))){
                                $this->showFormErrorMessages("error_file_ext","File tải lên không đúng định dạng!");
                                return ;
                            }
                            $file_name = UP_FOLDER."/user_ava/" . String::_name_cleaner(Unicode::stripUnicode($full_name),'-').'.'.$file_ext ;
                            $sourceName = $_FILES["avatar"]["tmp_name"];
                            if(!Upload::storage_put_file($file_name, $sourceName, IMAGE_SERVER_NO, FTP_BINARY,1)){
                                $this->showFormErrorMessages("error_upload_file","Có lỗi xảy ra khi upload file!");
                                return ;
                            };
                            $avatar_url = $file_name;
                        }
                    }
                }else{
                    $avatar_url = '';
                }
            }else{
                $avatar_url = '';
            }

            if(!isset($_POST['email_subcribe'])){
                $email_subcribe = 0;
            }
            else{
                $email_subcribe = 1;
            }

            $new_row=array	(
                'email'             =>  $email,
                'password'          =>  $password,
                'full_name'			=>	$full_name,
                'email_subcribe'	=>	$email_subcribe,
                'mobile_phone'		=>	$mobile_phone,
                'birth_day'         =>  $birth_day,
                'gender'            =>  $gender,
                'address'           =>  $address,
                'website'           =>  $website,
                'yahoo_id'          =>  $yahoo_id,
                'skype_id'          =>  $skype_id,
                'gids'             =>   $gids,
            );
            if($avatar_url != ''){
                $new_row = array_merge($new_row,array('avatar_url' => $avatar_url));
            }
            $user_id = DB::insert('user',$new_row);
        }
        //Url::redirect_current(array('cmd' => 'edit', 'user_id' => $user_id));
        Url::redirect_url('user.html');
    }

    function draw()
    {
        global $display;
        $groups = User::get_system_groups();
        $display->add('groups',$groups);
        $display->add('msg',$this->showFormErrorMessages(1));
        $display->add('begin_form',$this->beginForm(true,'POST',false,false,1));
        $display->add('end_form',$this->endForm(1));
        $display->output('add_user');
    }
}
?>
